﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace tablegen2.logic
{
    public class TableExcelExportCS
    {
        private static readonly string s_CodeDataTemplate = "\\CodeDataTemplate.txt";
        private static readonly string s_Timer = "__DATA_TABLE_CREATE_TIME__";
        private static readonly string s_NameSpace = "__DATA_TABLE_NAME_SPACE__";
        private static readonly string s_ClassName = "__DATA_TABLE_CLASS_NAME__";
        private static readonly string s_ClassDescription = " __DATA_TABLE_COMMENT__";
        private static readonly string s_TableProperty = "__DATA_TABLE_PROPERTIES__";
        private static readonly string s_TableParser = "__DATA_TABLE_PARSER__";
        private static readonly string s_Table = "\t";
        private static readonly string s_Space = "\r";

        private static StringBuilder ReadCodeTemplate()
        {
            string path =Util.WorkPath + s_CodeDataTemplate;

            if (!File.Exists(path))
            {
                throw new Exception($"config path '{path }' in not exists {s_CodeDataTemplate}file");
            }


            string[] data =  File.ReadAllLines(path);

            StringBuilder sb = new StringBuilder(); ;

            foreach (var str in data)
            {
                sb.Append(str);
                sb.AppendLine();
            }

            return sb;

        
        }
        public static void exportExcelFile(TableExcelData data, string filePath)
        {

            StringBuilder sb = ReadCodeTemplate();

            sb.Replace(s_Timer, DateTime.Now.ToString());
            sb.Replace(s_NameSpace, "App.DataTable");
            sb.Replace(s_ClassName, Path.GetFileName(filePath).Split('.')[0].Trim());
            sb.Replace(s_ClassDescription,"");
            sb.Replace(s_TableProperty, TableProperty(data));
            sb.Replace(s_TableParser, TableParser(data));

            // using (StreamWriter w = File.CreateText(filePath))
            // {
            //     for (int i = 0; i < str.Length; i++)
            //     {
            //         w.WriteLine(str[i]);
            //     }
            //     //刷新缓存
            //     w.Flush();
            //   
            // }

            File.WriteAllBytes(filePath, Encoding.UTF8.GetBytes(sb.ToString()));
        }


        public static string TableProperty(TableExcelData data)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < data.Headers.Count; i++)
            {
                var hdr = data.Headers[i];

                sb.AppendLine($" {s_Table} {s_Table}/// <summary>");
                sb.AppendLine($" {s_Table} {s_Table}/// {data.Headers[i].FieldDesc.Trim()}");
                sb.AppendLine($" {s_Table} {s_Table}/// <summary>");
                sb.AppendLine($" {s_Table} {s_Table}public {hdr.FieldType.ToLower().Trim()} {hdr.FieldName.Trim()}  {{ get; private set; }}");
                sb.AppendLine();
            }

            return sb.ToString();
        }

        public static string TableParser(TableExcelData data)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine($" {s_Table} {s_Table}public override void InitData(Dictionary<string, object> data)");
            sb.AppendLine($" {s_Table} {s_Table}{{");
            //sb.AppendLine($" {s_Table} {s_Table} {s_Table}" + @"ID = Convert.ToInt32(data[""ID""]);" );

      
            for (int i = 0; i < data.Headers.Count; i++)
            {
                var hdr = data.Headers[i];
                string str = hdr.FieldName;

                switch (hdr.FieldType.ToLower())
                {
                    case "string":
                        sb.AppendLine($" {s_Table} {s_Table} {s_Table}" + $@"{str} = Convert.ToString(data[""{str}""]);");

                        break;
                    case "int":
                        sb.AppendLine($" {s_Table} {s_Table} {s_Table}" + $@"{str} = Convert.ToInt32(data[""{str}""]);");

                        break;
                    case "float":
                        sb.AppendLine($" {s_Table}{s_Table} {s_Table}" + $@"{str} = float.Parse(data[""{str}""], CultureInfo.InvariantCulture);");
                        break;

                    case "long":
                        sb.AppendLine($" {s_Table} {s_Table} {s_Table}" + $@"{str} = Convert.ToInt64(data[""{str}""]);");
                        break;
                    case "string[]":
                        sb.AppendLine($" {s_Table} {s_Table} {s_Table}" + $@"{str} = Utility.Unity.Convert.ToStringArray(data[""{str}""]);");
                        break;
                    case "int[]":
                        sb.AppendLine($" {s_Table} {s_Table} {s_Table}" + $@"{str} = Utility.Unity.Convert.ToIntArray(data[""{str}""]);");
                        break;
                    case "long[]":
                        sb.AppendLine($" {s_Table} {s_Table} {s_Table}" + $@"{str} = Utility.Unity.Convert.ToLongArray(data[""{str}""]);");
                        break;

                }

            }

            sb.Append($" {s_Table} {s_Table}}}");
            return sb.ToString();
        }
    }
}
